package com.fhnew.app.ui.activity.userinfo.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fhnew.app.BuildConfig;
import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResRecord;
import com.fhnew.app.model.network.entity.req.ResdrawRecord;
import com.fhnew.app.model.network.entity.req.RespayRecord;

import com.fhnew.app.util.OnItemClickListener;

import java.util.ArrayList;

import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/12.
 */

public class UsermenueFragment extends Fragment {

    TextView addmoneybt;
    TextView qitabt;
    RecyclerView menuelist;
    Unbinder unbinder;
    LinearLayout menuelayout1;
    LinearLayout menuelayout2;
    LinearLayout menuelayout3;
    TextView menuestarttime;
    TextView menueendtime;
    TextView menuesearch;
    TextView getmoneybt;
    TextView menueclear;

    private ArrayList<ResRecord.DataBean> resRecords = new ArrayList<>();
    private ArrayList<RespayRecord.DataBean> payrecords = new ArrayList<>();
    private ArrayList<ResdrawRecord.DataBean> resdrawrecord = new ArrayList<>();
    Adapter adapter;
    Adapter1 adapter1;
    Adapter2 adapter2;

//    @Override
//    protected int setContentView () {
//        return R.layout.usermenuelayout;
//    }

    private void initView (View view) {
        addmoneybt = view.findViewById(R.id.addmoneybt);
        qitabt = view.findViewById(R.id.qitabt);
        menuelist = view.findViewById(R.id.menuelist);
        menuelayout1 = view.findViewById(R.id.menuelayout1);
        menuelayout2 = view.findViewById(R.id.menuelayout2);
        menuelayout3 = view.findViewById(R.id.menuelayout3);
        menuestarttime = view.findViewById(R.id.menuestarttime);
        menueendtime = view.findViewById(R.id.menueendtime);
        menuesearch = view.findViewById(R.id.menuesearch);
        getmoneybt = view.findViewById(R.id.getmoneybt);
        menueclear = view.findViewById(R.id.menueclear);

        addmoneybt.setOnClickListener(onClickListener);
        getmoneybt.setOnClickListener(onClickListener);
        qitabt.setOnClickListener(onClickListener);
        menuestarttime.setOnClickListener(onClickListener);
        menueendtime.setOnClickListener(onClickListener);
        menuesearch.setOnClickListener(onClickListener);
        menueclear.setOnClickListener(onClickListener);

    }

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.usermenuelayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onHiddenChanged (boolean hidden) {
        super.onHiddenChanged(hidden);
        getuseraddmenue();
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getuseraddmenue();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        menuelist.setLayoutManager(layoutManager);
        adapter1 = new Adapter1();
        menuelist.setAdapter(adapter1);
        if (BuildConfig.CASH) {
            getmoneybt.setVisibility(View.VISIBLE);
        } else {
            getmoneybt.setVisibility(View.GONE);
        }
    }

    private void getuseraddmenue () {
        HttpJsonUtils.getHttpApiInstance()
                .payRecord(Constants.getuseAdress() + "api/m/pc/record/payRecord", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<RespayRecord>(UsermenueFragment.this) {
                .subscribe(new CustomObserver<RespayRecord>() {
                    @Override
                    protected void onCustomNext (RespayRecord resALLGames) {
                        if (resALLGames.code == 0) {
                            payrecords.clear();
                            payrecords.addAll(resALLGames.data);
                            adapter1.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    private View.OnClickListener onClickListener =new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.addmoneybt:
                    menuelist.setAdapter(adapter1);
                    getuseraddmenue();
//                flag = true;
                    menuelayout1.setVisibility(View.VISIBLE);
                    menuelayout2.setVisibility(View.GONE);
                    menuelayout3.setVisibility(View.GONE);
                    addmoneybt.setBackgroundResource(R.drawable.textbackground5);
                    qitabt.setBackgroundResource(R.drawable.textbackground4);
                    getmoneybt.setBackgroundResource(R.drawable.textbackground4);
                    break;
                case R.id.qitabt:
                    adapter = new Adapter();
                    menuelist.setAdapter(adapter);
//                flag = false;
                    menuelayout1.setVisibility(View.GONE);
                    menuelayout3.setVisibility(View.GONE);
                    menuelayout2.setVisibility(View.VISIBLE);
                    qitabt.setBackgroundResource(R.drawable.textbackground5);
                    addmoneybt.setBackgroundResource(R.drawable.textbackground4);
                    getmoneybt.setBackgroundResource(R.drawable.textbackground4);
                    getotherRecord();
                    break;
                case R.id.getmoneybt:
                    adapter2 = new Adapter2();
                    menuelist.setAdapter(adapter2);
                    menuelayout1.setVisibility(View.GONE);
                    menuelayout2.setVisibility(View.GONE);
                    menuelayout3.setVisibility(View.VISIBLE);
                    getmoneybt.setBackgroundResource(R.drawable.textbackground5);
                    addmoneybt.setBackgroundResource(R.drawable.textbackground4);
                    qitabt.setBackgroundResource(R.drawable.textbackground4);
                    getWithDrawList();
                    break;
                case R.id.menuestarttime:
                    break;
                case R.id.menueendtime:
                    break;
                case R.id.menuesearch:
                    break;
                case R.id.menueclear:
                    break;
            }
        }
    };




    private void getWithDrawList () {

        HttpJsonUtils.getHttpApiInstance()
                .drawRecord(Constants.getuseAdress() + "api/m/pc/record/drawRecord", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResdrawRecord>(UsermenueFragment.this) {
                .subscribe(new CustomObserver<ResdrawRecord>() {
                    @Override
                    protected void onCustomNext (ResdrawRecord resRecord) {
                        if (resRecord.code == 0 && resRecord.data.size() != 0) {
                            resdrawrecord.clear();
                            resdrawrecord.addAll(resRecord.data);
                            adapter2.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    private void getotherRecord () {
        HttpJsonUtils.getHttpApiInstance()
                .getotherRecord(Constants.getuseAdress() + "api/m/pc/record/otherRecord", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResRecord>(UsermenueFragment.this) {
                .subscribe(new CustomObserver<ResRecord>() {
                    @Override
                    protected void onCustomNext (ResRecord resRecord) {
                        if (resRecord.code == 0 && resRecord.data.size() != 0) {
                            resRecords.clear();
                            resRecords.addAll(resRecord.data);
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    //////////////////////////////////////其他////////////////////////////////////////////
    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.otheritem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (final MViewHolder holder, final int position) {
            holder.ordernum.setText("" + (position + 1));
            holder.number.setText(resRecords.get(position).type);
            holder.money.setText(resRecords.get(position).money);
            String str = resRecords.get(position).created_at.substring(0, 10);
            holder.time.setText(str);
            holder.cotent.setText(resRecords.get(position).playcate_name);
            if (position % 2 != 0) {
                holder.menuelayout2.setBackgroundResource(R.color.blue);
            } else {
                holder.menuelayout2.setBackgroundResource(R.color.royalblue);
            }
        }

        @Override
        public int getItemCount () {
            return resRecords.size();

        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            LinearLayout menuelayout2;
            TextView cotent;
            TextView ordernum;
            TextView number;
            TextView money;
            TextView time;

            MViewHolder (View itemView) {
                super(itemView);
                menuelayout2 = (LinearLayout) itemView.findViewById(R.id.menuelayout2);
                ordernum = (TextView) itemView.findViewById(R.id.ordernum);
                number = (TextView) itemView.findViewById(R.id.number);
                money = (TextView) itemView.findViewById(R.id.money);
                time = (TextView) itemView.findViewById(R.id.time);
                cotent = (TextView) itemView.findViewById(R.id.cotent);
            }
        }
    }

    ///////////////////////////////////////////充值//////////////////////////////////////////////
    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter1.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter1.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.payitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (final MViewHolder holder, final int position) {
            holder.ordernum.setText("" + (position + 1));
            holder.number.setText(payrecords.get(position).orderNum);
            holder.money.setText(payrecords.get(position).amount);
            String str = payrecords.get(position).created_at.substring(0, 10);
            holder.time.setText(str);
            if (position % 2 != 0) {
                holder.menuelayout2.setBackgroundResource(R.color.blue);
            } else {
                holder.menuelayout2.setBackgroundResource(R.color.royalblue);
            }
        }

        @Override
        public int getItemCount () {
            return payrecords.size();

        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            LinearLayout menuelayout2;
            TextView ordernum;
            TextView number;
            TextView money;
            TextView time;

            MViewHolder (View itemView) {
                super(itemView);
                menuelayout2 = (LinearLayout) itemView.findViewById(R.id.menuelayout2);
                ordernum = (TextView) itemView.findViewById(R.id.ordernum);
                number = (TextView) itemView.findViewById(R.id.number);
                money = (TextView) itemView.findViewById(R.id.money);
                time = (TextView) itemView.findViewById(R.id.time);
            }
        }
    }

    ///////////////////////////////////////////提现//////////////////////////////////////////////
    class Adapter2 extends RecyclerView.Adapter<Adapter2.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter2.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter2.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.otheritem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (final MViewHolder holder, final int position) {
            holder.ordernum.setText("" + (position + 1));
            holder.number.setText(resdrawrecord.get(position).draw_typeName);
            holder.money.setText(resdrawrecord.get(position).amount);
            String str = resdrawrecord.get(position).created_at.substring(0, 10);
            holder.time.setText(str);
            holder.cotent.setText(resdrawrecord.get(position).statusName);
            if (position % 2 != 0) {
                holder.menuelayout2.setBackgroundResource(R.color.blue);
            } else {
                holder.menuelayout2.setBackgroundResource(R.color.royalblue);
            }
        }

        @Override
        public int getItemCount () {
            return resdrawrecord.size();

        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            LinearLayout menuelayout2;
            TextView cotent;
            TextView ordernum;
            TextView number;
            TextView money;
            TextView time;

            MViewHolder (View itemView) {
                super(itemView);
                menuelayout2 = (LinearLayout) itemView.findViewById(R.id.menuelayout2);
                ordernum = (TextView) itemView.findViewById(R.id.ordernum);
                number = (TextView) itemView.findViewById(R.id.number);
                money = (TextView) itemView.findViewById(R.id.money);
                time = (TextView) itemView.findViewById(R.id.time);
                cotent = (TextView) itemView.findViewById(R.id.cotent);
            }
        }
    }


}
